/**
 *  Copyright (c) 2007 - 2009 Adobe
 *  All rights reserved.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 *  IN THE SOFTWARE.
 */
package com.adobe.txi.todo.application.authentication
{
    import mx.messaging.ChannelSet;
    import mx.messaging.config.ServerConfig;
    import mx.rpc.AsyncResponder;
    import mx.rpc.AsyncToken;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    import org.spicefactory.lib.task.ResultTask;

    public class LoginChannelSetTask extends ResultTask
    {
        private var service:RemoteObject;

        private var username:String;

        private var password:String;

        public function LoginChannelSetTask(service:RemoteObject, username:String,
                                            password:String)
        {
            super();

            this.service = service;

            this.username = username;
            this.password = password;
        }

        protected override function doStart():void
        {
            try
            {
                //var channelSet:ChannelSet = service.channelSet;
                var channelSet:ChannelSet = ServerConfig.getChannelSet("loginService");
                var token:AsyncToken = channelSet.login(username, password);

                token.addResponder(new AsyncResponder(onResult, onError));
            }
            catch (e:Error)
            {
                error(e.message);
            }
        }


        private function onResult(event:ResultEvent, token:AsyncToken):void
        {
            setResult(null);
        }

        private function onError(event:FaultEvent, token:AsyncToken):void
        {
            error(event.fault.faultDetail);
        }
    }
}